package com.luo.service;

import com.luo.dao.impl.CartDaoImpl;
import com.luo.model.Carts;
import com.luo.model.CartsVO;

import java.util.List;
import java.util.Map;

/**
 * 功能：
 * 作者：Luo。
 * 日期：2024/2/13 16:09
 */
public class CartService {
	CartDaoImpl cartDao = new CartDaoImpl();

	public List<Carts> queryCartsById(String id) {
		List<Carts> carts = cartDao.queryCartsById(id);
		for (Carts c : carts) {
			c.setSelected((c.getSelectedNum() == 1 ? true : false));
		}
		return carts;
	}

	public int addOneCart(Map map) {
		int i = cartDao.addOneCart(map);
		return i;
	}

	public int deleteOneCartByIds(Map map) {
		int i = cartDao.deleteOneCartByIds(map);
		return i;
	}

	public int updateOneCartById(Map map) {
		int i = cartDao.updateOneCartById(map);
		return i;
	}

	public int updateOneCartSelectedById(Map map) {
		boolean selected = (boolean) Boolean.parseBoolean((String) map.get("selected"));
		if (selected) {
			map.put("selected", 1);
		} else {
			map.put("selected", 0);
		}
		int i = cartDao.updateOneCartSelectedById(map);
		return i;
	}

	public List<CartsVO> queryCartsVOByUserId(String id) {
		List<CartsVO> list = cartDao.queryCartsVOByUserId(id);
		for (CartsVO c : list) {
			Double disCount = Double.valueOf(c.getGoodDiscount()) / 10;
			if (c.getGoodDiscount() < 10 && c.getGoodDiscount() > 0) {
				c.setTotalPrice(Double.valueOf(String.format("%.2f", c.getGoodPrice() * disCount)) * c.getCount());
				c.setGoodPrice(Double.valueOf(String.format("%.2f", c.getGoodPrice() * disCount)));
			} else {
				c.setTotalPrice(c.getGoodPrice() * c.getCount());
			}
		}
		return list;
	}

	public Carts queryCartsByCartsId(String id) {
		Carts cart = cartDao.queryCartsByCartsId(id);
		// for (Carts c : carts) {
		// 	c.setSelected((c.getSelectedNum() == 1 ? true : false));
		// }
		return cart;
	}
}
